import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
var _excluded = ["label", "key"];
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { DownOutlined } from '@ant-design/icons';
import { Dropdown, Menu, Space, Tabs } from 'antd-v4';
import classNames from 'classnames';
import useMergedState from "rc-util/es/hooks/useMergedState";
import './index.less';

var HeaderMenu = function HeaderMenu(props) {
  var _props$items = props.items,
      items = _props$items === void 0 ? [] : _props$items,
      _props$type = props.type,
      type = _props$type === void 0 ? 'inline' : _props$type,
      prefixCls = props.prefixCls,
      propActiveKey = props.activeKey;

  var _useMergedState = useMergedState(propActiveKey, {
    value: propActiveKey,
    onChange: props.onChange
  }),
      _useMergedState2 = _slicedToArray(_useMergedState, 2),
      activeKey = _useMergedState2[0],
      setActiveKey = _useMergedState2[1];

  if (items.length < 1) {
    return null;
  }

  var activeItem = items.find(function (item) {
    return item.key === activeKey;
  }) || items[0];

  if (type === 'inline') {
    return _jsx("div", {
      className: classNames("".concat(prefixCls, "-menu"), "".concat(prefixCls, "-inline-menu")),
      children: items.map(function (item, index) {
        return _jsx("div", {
          onClick: function onClick() {
            setActiveKey(item.key);
          },
          className: classNames("".concat(prefixCls, "-inline-menu-item"), activeItem.key === item.key ? "".concat(prefixCls, "-inline-menu-item-active") : undefined),
          children: item.label
        }, item.key || index);
      })
    });
  }

  if (type === 'tab') {
    return _jsx(Tabs, {
      activeKey: activeItem.key,
      onTabClick: function onTabClick(key) {
        return setActiveKey(key);
      },
      children: items.map(function (_ref, index) {
        var label = _ref.label,
            key = _ref.key,
            rest = _objectWithoutProperties(_ref, _excluded);

        return _jsx(Tabs.TabPane, _objectSpread({
          tab: label
        }, rest), key || index);
      })
    });
  }

  return _jsx("div", {
    className: classNames("".concat(prefixCls, "-menu"), "".concat(prefixCls, "-dropdownmenu")),
    children: _jsx(Dropdown, {
      trigger: ['click'],
      overlay: _jsx(Menu, {
        selectedKeys: [activeItem.key],
        onClick: function onClick(item) {
          setActiveKey(item.key);
        },
        items: items.map(function (item, index) {
          return {
            key: item.key || index,
            disabled: item.disabled,
            label: item.label
          };
        })
      }),
      children: _jsxs(Space, {
        className: "".concat(prefixCls, "-dropdownmenu-label"),
        children: [activeItem.label, _jsx(DownOutlined, {})]
      })
    })
  });
};

export default HeaderMenu;